package com.study.hisapi.exception;

import com.study.hisapi.domain.bo.core.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异处理
 *
 * @author LiXiao
 * @date 2024/4/11
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * HIS 请求发生异常
     */
    @ExceptionHandler(HisRequestException.class)
    public R<Void> handleHisRequestException(HisRequestException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',HIS请求异常'{}'", requestURI, e.getMessage());
        return R.fail(e.getErrorCode(), "HIS调用失败 " + e.getMessage());
    }

    /**
     * 数据验证异常
     */
    @ExceptionHandler(ValidatedException.class)
    public R<Void> handleValidatedException(ValidatedException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',数据验证失败'{}'", requestURI, e.getMessage());
        return R.fail(e.getErrorCode(), "数据验证失败 " + e.getMessage());
    }

    /**
     * 方法未重写异常
     */
    @ExceptionHandler(MethodNotOverriddenException.class)
    public R<Void> handleMethodNotOverriddenException(MethodNotOverriddenException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',方法未重写'{}'", requestURI, e.getMessage());
        return R.fail(e.getErrorCode(), "方法未重写 " + e.getMessage());
    }

    /**
     * 方法未定义
     */
    @ExceptionHandler(NotFoundException.class)
    public R<Void> handleNotFoundException(NotFoundException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',方法未定义'{}'", requestURI, e.getMessage());
        return R.fail(e.getErrorCode(), "方法未定义 " + e.getMessage());
    }

    /**
     * 方法未定义
     */
    @ExceptionHandler(ServiceException.class)
    public R<Void> handleServiceException(ServiceException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',未捕获的异常，统一抛出该异常信息'{}'", requestURI, e.getMessage());
        return R.fail(e.getErrorCode(), "未捕获的异常 " + e.getMessage());
    }
}
